/**
 * 
 */
package mtsu.spring11.rnasearch;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
 * @author Daniel
 *
 */
public class Searcher {
	private final static ConcurrentLinkedQueue<TargetSequence> workQueue = new ConcurrentLinkedQueue<TargetSequence>();
	private final static ConcurrentLinkedQueue<HitInfo> hitQueue = new ConcurrentLinkedQueue<HitInfo>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TargetSequence temp = new TargetSequence("target gene 1","CATCATAGTATGATGAGGGATGATGCACTAC");
		RuleSet tempRule = new RuleSet();
		int numRules = 2;
		int numWind = 2;
		int lengthRule = 6;
		String[] names = new String[2];
		names[0] = "ebv-miR-BART7";
		names[1] = "hsa-miR-142-3p";
		String[][] rules = new String[2][2];
		rules[0][0] = "ATGATG";
		rules[0][1] = "TATGAT";
		rules[1][0] = "ACTACA";
		rules[1][1] = "CACTAC";
		tempRule.lengthRule = lengthRule;
		tempRule.names = names;
		tempRule.numRules = numRules;
		tempRule.numWind = numWind;
		tempRule.rules = rules;
		
		search(temp,tempRule);
		
		while(!hitQueue.isEmpty())
		{
			HitInfo tempHit = hitQueue.poll();
			System.out.printf("%s, %s, %s\n", tempHit.targetName, tempHit.ruleSeq, tempHit.ruleName);
		}

	}
	
	private static void search(TargetSequence tar, RuleSet rul)
	{
		int length = tar.sequenceLength;
		int j = rul.numRules;
		int l = rul.numWind;
		int k = rul.lengthRule;
		int[][] test = new int[j][l]; // default initalization to 0.
		char curr;
		for(int i = 0; i < length; i++)
		{
			curr = tar.geneSequence.charAt(i);
			for(int w = 0; w < j; w++)
			{
				for(int x = 0; x < l; x++)
				{
					if(curr == rul.rules[w][x].charAt(test[w][x])) // if match, increment
					{
						test[w][x]++;
						if(test[w][x] == k)
						{
							HitInfo temp = new HitInfo(tar.geneName, rul.names[w], rul.rules[w][x], x, k, curr + 1 - k);
							hitQueue.add(temp);
							test[w][x] = 0;
						}
					}
					else if(test[w][x] != 0) // if not a match and not 0, set to 0 and check again.
					{
						test[w][x] = 0;
						if(curr == rul.rules[w][x].charAt(test[w][x]))
						{
							test[w][x]++;
						}
					}
					else
					{
						
					}
				}
				
			}
		}
	}
}
